package rensoft.android.view;

import static rensoft.java.lang.Lang.is;

import android.app.AlertDialog.Builder;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.os.Bundle;
import rensoft.android.HasActivity;
import rensoft.android.HasActivityPresenter;
import rensoft.android.lang.AndroidLang;
import rensoft.java.lang.Factory;

public class ErrorDialogPresenter extends HasActivityPresenter implements Factory<Dialog> {

  private final DialogPresenter errorDialogPresenter = new DialogPresenter(this);
  private int dialogTitleId;
  private String email_text;
  private final String supportEmail;
  private final int emailTitle;
  private final int mailActivityChooserTitle;
  private final int sendButtonText;

  public ErrorDialogPresenter(HasActivity hasActivity, int sendButtonText, String supportMail,
      int emailTitle, int mailActivityChooserTitle) {
    super(hasActivity);
    this.sendButtonText = sendButtonText;
    supportEmail = supportMail;
    this.emailTitle = emailTitle;
    this.mailActivityChooserTitle = mailActivityChooserTitle;
  }

  @Override
  public Dialog create() {
    Builder builder = new Builder(activity);
    builder.setMessage(dialogTitleId);
    builder.setPositiveButton(sendButtonText, new OnClickListener() {
      public void onClick(DialogInterface arg0, int arg1) {
        onSendButonClick();
      }
    });
    return builder.create();
  }

  public void showErrorDialog(int messageTitleId, String error_message, String error_details) {
    if (is(error_message)) email_text += "\n\n Message: " + error_message;
    if (is(error_details)) email_text += "\n\n Details:" + error_details;
    if (errorDialogPresenter.isDialogVisible()) return;
    dialogTitleId = messageTitleId;
    errorDialogPresenter.setTitleId(dialogTitleId);
    errorDialogPresenter.showDialog();
  }

  @Override
  protected void onCreateRestore(Bundle state) {
    super.onCreateRestore(state);
    errorDialogPresenter.setTitleId(dialogTitleId);
  }

  private void onSendButonClick() {
    sendMailToSupport();
  }

  private void sendMailToSupport() {
    Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
    emailIntent.setType("plain/text");
    emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[] {supportEmail});
    emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, getString(emailTitle) + " "
        + AndroidLang.getAplication().getApplicationName());
    emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, email_text);
    startActivity(Intent.createChooser(emailIntent, getString(mailActivityChooserTitle)));
  }

}
